import axios from 'axios'
import qs from 'qs'
import ep from '../utils/ep'

axios.defaults.baseURL = "http://120.194.198.44:9100/sqrs/";
axios.defaults.headers.post['Content-Type'] = 'application/text;charset=UTF-8';
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*/*';

axios.interceptors.request.use(config => {
    // Indicator.open()
    return config
}, error => {
    return Promise.reject(error)
});

export default {
    //get请求
    get(url, param) {
        return new Promise((resolve, reject) => {
            ep.showLoading();
            axios({
                method: 'get',
                url,
                params: {
                    ...param,
                }
            }).then(res => {
                ep.hideLoading();
                resolve(res.data)
            }).catch((err) => {
                ep.hideLoading();
                let code = err.response.status;
                let param = {
                    msg: '连接错误' + code,
                    code: code
                }
                switch (code) {
                    case 401:
                        param = {
                            msg: '未授权，请重新登录',
                            code: 401
                        }
                        reject(param);
                        break;
                    case 501: case 502: case 503: case 504:
                        param = {
                            msg: '网络错误',
                            code: 501
                        }
                        reject(param);
                        break;
                    default:
                        reject(param);
                        break;
                }
            });
        });
    },
    //post请求
    post(url, param) {
        param = {
            ...param
        }
        console.log(param)
        return new Promise((resolve, reject) => {
            ep.showLoading();
            axios.post(url,param).then(res => {
                ep.hideLoading();
                resolve(res.data)
            }).catch((err) => {
                ep.hideLoading();
                reject(err.response.status);
            });
        });
    }
}
